//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    motto: '深度票据网 金秋送祝福',
    stars:[
      { size: [59, 29], img: ['star_1_1.png'], top: 0, right: 0, speed: 0.01, index: 1,type:1 },
      { size: [59, 29], img: ['star_1_1.png'],top: 0, right: 0, speed: 0.01, index: 2, type: 1 },
    ],
    moveStart:{},
    openFlag:false,//打开标记 只打开一次
    lights:{
      a: { opacity: 1, bottom: 0, opacitySpeed: 0.01, speed:3,limit:500},
      b: { opacity: 1, bottom: 0, opacitySpeed: 0.02, speed: 4, limit: 800},
      c: { opacity: 1, bottom: 0, opacitySpeed: 0.03, speed: 5, limit: 600}
    },
    zhufu:{//祝福语
      opacity:0,
    },
    upBottom:60,
    innerAudioContext:null,
    spiritStar:{
      big:{
        size: [[20, 10], [21, 12], [27, 14], [60, 32], [50, 23], [34, 19]],
        imgs: [['star_1_1.png'], ['star_1_2.png'], ['star_1_3.png'],['star_1_4.png'],['star_1_5.png'],['star_1_6.png']],
        top:0,
        right:0,
        speed:0.01,
        index:1,
        next:function(){

        }
      },
      small:{
        size: [[53,28], [147, 67], [75, 37], [84, 37], [186, 37], [93, 46]],
        img: [['star_2_1.png'], ['star_2_2.png'], ['star_2_3.png'], ['star_2_4.png'], ['star_2_5.png'], ['star_2_6.png']],
        top: 0,
        right: 0,
        speed: 0.02, index: 1,
      },
      mid:{
        size: [[59,29], [46, 22], [62, 26], [93, 43], [65,32]],
        img: [['star_3_1.png'], ['star_3_2.png'], ['star_3_3.png'], ['star_3_4.png'], ['star_3_5.png']],
        top: 0,
        right: 0,
        speed: 0.01, 
        index: 1,
      }
    },
   
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  //  setTimeout(function () {wx.navigateTo({url: '../problems/problems'})}, 6000)
    if (!this.innerAudioContext ){
       this.innerAudioContext = wx.createInnerAudioContext()
      this.innerAudioContext.autoplay = true
      this.innerAudioContext.src = 'http://www.shendupiaoju.com/uploadFile/wxspace/zhongqiu/zhongqiu.mp3'
       this.innerAudioContext.onPlay(() => {
        console.log('开始播放')
      })
      this.innerAudioContext.onError((res) => {
        console.log(res.errMsg)
        console.log(res.errCode)
      })
    }else{
    //  / this.innerAudioContext.play();
    }
    this.setData({
      openFlag: false
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      lights: {
        a: { opacity: 1, bottom: 0, opacitySpeed: 0.01, speed: 3, limit: 500 },
        b: { opacity: 1, bottom: 0, opacitySpeed: 0.02, speed: 4, limit: 800 },
        c: { opacity: 1, bottom: 0, opacitySpeed: 0.03, speed: 5, limit: 600 }
      },
      zhufu: {//祝福语
        opacity: 0,
      },
      openFlag:false
    });

    //this.innerAudioContext.stop();

  },
 
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    var thisObj = this; 
    var  i=60;
    var ext=3;
    setInterval(function(){
      var data=thisObj.data;
      thisObj.setData({
        lights: thisObj.dealLight(data), 
        stars: thisObj.dealStars(data),
        zhufu: { opacity: data.zhufu.opacity+0.018},
      })
    },100)
    setInterval(function () {
      i = i + ext;
      if(i>85){ext=-3;}
      if(i<60){ext = 3;}
      thisObj.setData({upBottom: i})}, 80)
  },

  saveImg:function(){
    wx.saveImageToPhotosAlbum({
      success(res) {}
    })
  },
  dealStars:function(data){
    var stars = data.stars;
    for(var i=0;i<stars.length;i++){
      stars[i].index = stars[i].index + 1;
      if (stars[i].index > stars[i].size.length) {
        stars[i].index = 1;
      }
    }
    return stars;
  },
 
  dealLight: function (data){
    var lights = data.lights;
    this.runLight(lights.a);
    this.runLight(lights.b);
    this.runLight(lights.c);
    return lights;
  },
  runLight: function (light){
    if (light.bottom > light.limit) {
      light.bottom = 0;
      light.opacity = 1;
    }
    if (light.bottom > light.limit-120) {
      light.opacity = light.opacity - light.opacitySpeed;
    }
    light.bottom = light.bottom + light.speed;
  },

  handletouchmove: function (event) {
    let currentX = event.touches[0].pageX;
    let currentY = event.touches[0].pageY;
    let tx = currentX - this.data.moveStart.x;
    let ty = currentY - this.data.moveStart.y;
    let text = "";
    var openFlag = this.data.openFlag;
    if (openFlag){
      return ;
    }
    openFlag=true;
    if (Math.abs(tx) > Math.abs(ty)) {
      if (tx < 0) {
        text = "向左滑动";
        this.data.flag = 1
      }
      else if (tx > 0) {
        text = "向右滑动";
        this.data.flag = 2
      }

    }
    //上下方向滑动
    else {
      if (ty < 0) {
        text = "向上滑动";
        this.data.flag = 3
        wx.redirectTo({ url: '../problems/problems' });
         console.info("------")
        this.setData({openFlag: openFlag})
      }
      else if (ty > 0) {
        text = "向下滑动";
        this.data.flag = 4
      }
    }
 
  },
  handletouchstart: function (event) {
    console.info(event.touches[0].pageX)
    this.setData({
      moveStart:{
        x: event.touches[0].pageX,
        y: event.touches[0].pageY
      }
    });
  },
  handletouchend: function (event) {
    this.data.flag = 0
    console.info( "没有滑动")
  }
})
